#include "bsp_main.h"   
#include "string.h"
#include "esp_log.h"
QueueHandle_t tm1650_led_queue =NULL;

// const uint8_t temp_set[3] = {60+8,70+8,80+8};

void bsp_main_init()
{
    vTaskDelay(500 / portTICK_PERIOD_MS);
    local_nvs_init();
    my_uart0_init();

    
    beep_init();
    button_initial();
    display_init();
    magnetic_init();
    current_initial();
    voice_init();


    blufi_init();
    mqtt_init();


    vTaskDelay(2000 / portTICK_PERIOD_MS);
    beep_output(BEEP_BUTTON);


}

static uint8_t diaplay_flash_status = FLASH_OFF;
void task_sysmain(void* arg)
{
    sys_main_param_t sys_main_param_old = {
        .sys_status = SYS_IDLE,
        .sys_mode_type_buffer = {60*60,POWER_0,POWER_0,POWER_0,POWER_0,POWER_0,POWER_0},
        .sys_pointer_index = MODE_TIME,
        .sys_vol_switch = MUSIC_CLOSE,
        .sys_countdown = 60 * 60,
        .sys_error_type = ERROR_TYPE_NO_ERROR,
        .ifappmqttconnect = MQTT_APP_DISCONNECT,
        .ifwificonnect = WIFI_DISCONNECT,
        .ifblufibleconnect = BLUFI_BLE_DISCONNECT,
    };
    sys_main_param_t* sys_task_param = Get_sys_param_pos();
    send_param_t* sys_send_param = Get_send_param_pos();
    uint8_t *queque_get = 0;
    uint8_t sys_need_display_flash = 0;
    uint8_t diaplay_flash_status_old = FLASH_OFF;
    for(;;){
        vTaskDelay(50 / portTICK_PERIOD_MS);

        if(memcmp(sys_task_param, &sys_main_param_old, sizeof(sys_main_param_t)) != 0){
            sys_need_display_flash = 1;
            /* MQTT连接状态 */
            if(sys_task_param->ifappmqttconnect != sys_main_param_old.ifappmqttconnect){
                if(sys_task_param->ifappmqttconnect == MQTT_CONNECT_PRIORITY_H){
                    sys_task_param->ifappmqttconnect = MQTT_APP_CONNECT;
                }
                if(sys_task_param->ifappmqttconnect == MQTT_APP_CONNECT){
                    mqtt_send(MQTT_SEND_D2S_QD, NULL);
                    mqtt_send(MQTT_SEND_D2S_GJ, NULL);
                }
            }
            /* BLUFI BLE 连接状态 */
            if(sys_task_param->ifblufibleconnect != sys_main_param_old.ifblufibleconnect){
                if(sys_task_param->ifblufibleconnect >= BLUFI_BLE_CONNECT){
                    if(sys_task_param->ifblufibleconnect == BLE_CONNECT_PRIORITY_H){
                        ble_registration_json_send();
                        sys_task_param->ifblufibleconnect = BLUFI_BLE_CONNECT;
                    }
                }

            }


            if(sys_main_param_old.sys_error_type != sys_task_param->sys_error_type){
                if(sys_task_param->sys_error_type != ERROR_TYPE_NO_ERROR){
                    sys_task_param->sys_status = SYS_PAUSE;
                    beep_output(BEEP_ERROR);
                }
            }


            if(sys_main_param_old.sys_countdown != sys_task_param->sys_countdown){
                if(sys_task_param->sys_countdown == 0){
                    sys_task_param->sys_status = SYS_STOP;
                    beep_output(BEEP_STOP);
                }

            }
            /* 音量开关 */
            if(sys_main_param_old.sys_vol_switch != sys_task_param->sys_vol_switch){
                voice_volume_shift(sys_task_param->sys_vol_switch);
            }



            if(memcmp(sys_task_param->sys_mode_type_buffer,sys_main_param_old.sys_mode_type_buffer,sizeof(sys_task_param->sys_mode_type_buffer)) != 0){

                
                if(sys_main_param_old.sys_mode_type_buffer[MODE_TIME] != sys_task_param->sys_mode_type_buffer[MODE_TIME]){
                    sys_task_param->sys_status = SYS_STOP;
                    sys_task_param->sys_countdown = sys_task_param->sys_mode_type_buffer[MODE_TIME];
                }
                if(sys_main_param_old.sys_mode_type_buffer[MODE_DLQD] != sys_task_param->sys_mode_type_buffer[MODE_DLQD]){
                    current_set_intensity(sys_task_param->sys_mode_type_buffer[MODE_DLQD]);
                }
                if(sys_main_param_old.sys_mode_type_buffer[MODE_DLPL] != sys_task_param->sys_mode_type_buffer[MODE_DLPL]){
                    current_set_frequecy(sys_task_param->sys_mode_type_buffer[MODE_DLPL]);
                }

                if((sys_main_param_old.sys_mode_type_buffer[MODE_CCQD] != sys_task_param->sys_mode_type_buffer[MODE_CCQD]) ||  \
                    (sys_main_param_old.sys_mode_type_buffer[MODE_CZFD] != sys_task_param->sys_mode_type_buffer[MODE_CZFD]) || \
                        (sys_main_param_old.sys_mode_type_buffer[MODE_CZPL] != sys_task_param->sys_mode_type_buffer[MODE_CZPL])){
                    // printf("\r\nCCQD=%d,czfd=%d,czpl=%d\r\n",sys_task_param->sys_mode_type_buffer[MODE_CCQD],sys_task_param->sys_mode_type_buffer[MODE_CZFD],sys_task_param->sys_mode_type_buffer[MODE_CZPL]);
                    // printf("\r\nCCQD=%d,czfd=%d,czpl=%d\r\n",);
                    magnetic_param_change(sys_task_param->sys_mode_type_buffer);
                }

            }

            if(sys_task_param->sys_status != sys_main_param_old.sys_status){
                switch(sys_task_param->sys_status){
                    case SYS_RUN_NORMAL:
                        sys_task_param->sys_mode_type_buffer[MODE_DLPL] = POWER_1;
                        sys_task_param->sys_mode_type_buffer[MODE_DLQD] = POWER_1;
                        current_output_start(sys_task_param->sys_mode_type_buffer[MODE_DLMS], sys_task_param->sys_mode_type_buffer[MODE_DLPL],
                                                sys_task_param->sys_mode_type_buffer[MODE_DLQD], sys_task_param->sys_vol_switch);
                        sys_task_param->sys_error_type = ERROR_TYPE_NO_ERROR;
                        break;
                    case SYS_STOP:
                        current_output_stop();
                        sys_task_param->sys_countdown = sys_task_param->sys_mode_type_buffer[MODE_TIME];
                        break;
                    case SYS_PAUSE:
                        current_output_stop();
                        break;
                    default:
                        break;

                }

            }

            sys_send_param->send_countdown = sys_task_param->sys_countdown;
            sys_send_param->send_ehz = sys_task_param->sys_mode_type_buffer[MODE_DLPL];
            sys_send_param->send_emode = sys_task_param->sys_mode_type_buffer[MODE_DLMS];
            sys_send_param->send_error_type = sys_task_param->sys_error_type;
            sys_send_param->send_este = sys_task_param->sys_mode_type_buffer[MODE_DLQD];
            sys_send_param->send_mamp = sys_task_param->sys_mode_type_buffer[MODE_CZFD];
            sys_send_param->send_mhz = sys_task_param->sys_mode_type_buffer[MODE_CZPL];
            sys_send_param->send_mste = sys_task_param->sys_mode_type_buffer[MODE_CCQD];
            sys_send_param->send_runtime = sys_task_param->sys_mode_type_buffer[MODE_TIME];
            sys_send_param->send_status = sys_task_param->sys_status;
            if(sys_task_param->sys_vol_switch == MUSIC_CLOSE){
                sys_send_param->send_vol = MUSIC_CLOSE;
                sys_send_param->send_music = MUSIC_CLOSE;
            }else{
                sys_send_param->send_music = MUSIC_OPEN;
                sys_send_param->send_vol = sys_task_param->sys_vol_switch;
            }

            /* 同步数据 */
            memcpy(&sys_main_param_old, sys_task_param, sizeof(sys_main_param_t));
            /* 发送状态 */
            if(sys_task_param->ifappmqttconnect == MQTT_APP_CONNECT){
                mqtt_send(MQTT_SEND_D2U_ZT, sys_send_param);
            }
            if(sys_task_param->ifblufibleconnect >= BLUFI_BLE_CONNECT){
                ble_report_json_send(sys_send_param);
            }
            
        }

        if((sys_need_display_flash == 1) || (diaplay_flash_status_old != diaplay_flash_status)){
            sys_need_display_flash = 0;
            diaplay_flash_status_old = diaplay_flash_status;
            queque_get = TM1652_led_datatrans(sys_task_param, diaplay_flash_status);
            xQueueSend(display_led_queue, queque_get, 0);
        }
    }
}



IRAM_ATTR bool my_timer_on_alarm_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
{
    static uint8_t times = 0;
    sys_main_param_t* timer1_sys_param = NULL;
    timer1_sys_param = Get_sys_param_pos();
    if(timer1_sys_param->sys_status == SYS_RUN_NORMAL){
        if(timer1_sys_param->sys_countdown == 0){
            return 1;
        }else{
            if(++times >= 2){
                timer1_sys_param->sys_countdown--;
                times = 0;
            }
        }
    }
    diaplay_flash_status = !diaplay_flash_status;
    /* mqttkeeplive计时 */
    if(timer1_sys_param->ifappmqttconnect == MQTT_APP_CONNECT){
        mqtt_keepalive_count++;
        if(mqtt_keepalive_count >= 2*10){
            timer1_sys_param->ifappmqttconnect = MQTT_APP_DISCONNECT;
            mqtt_keepalive_count = 0;
        }
    }else{
        mqtt_keepalive_count = 0;
    }

    return 1;
}













